/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.xml.parser.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.kopsox.util.xml.data.XMLValidatorDescriptor;
import org.kopsox.util.xml.data.impl.XMLValidatorDescriptorImpl;
import org.kopsox.util.xml.parser.XMLEnum;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * @author Konrad Renner - 23.07.2010
 *
 */
public class ValidatorXMLParser extends AbstractKopsoxXMLParser {
	
	private final List<XMLValidatorDescriptor> descriptors;
	private int currentIndex;		//Saves the position in the List, of the validator which was just created

	public ValidatorXMLParser(String file) {
		super(file);
		this.descriptors = new ArrayList<XMLValidatorDescriptor>();
		this.currentIndex = -1;
	}
	
	
	public List<XMLValidatorDescriptor> getValidatorDescriptors() {
		return this.descriptors;
	}
	
	@Override 
	  public void startElement( String namespaceURI, String localName, String qName, Attributes atts ) throws SAXException 
	  { 
		    /*System.out.println( "namespaceURI: " + namespaceURI ); 
		    System.out.println( "localName: " + localName ); 
		    System.out.println( "qName: " + qName ); 
		 
		    for ( int i = 0; i < atts.getLength(); i++ ) 
		      System.out.printf( "Attribut no. %d: %s = %s%n", i, 
		                         atts.getQName( i ), atts.getValue( i ) ); */
		    
		    if(XMLEnum.VALIDATOR.toString().equals(qName)) {
		    	String name = atts.getValue(0);
		    	XMLValidatorDescriptorImpl validator = new XMLValidatorDescriptorImpl();
		    	validator.setName(name);
		    	this.descriptors.add(validator);
		    	this.currentIndex++;
		    }else if(XMLEnum.TYPE.toString().equals(qName)) {
		    	XMLValidatorDescriptorImpl impl = (XMLValidatorDescriptorImpl)this.descriptors.get(this.currentIndex);
		    	
		    	impl.setClassName(atts.getValue(0));
		    }else if(XMLEnum.HANDLER.toString().equals(qName)) {
		    	XMLValidatorDescriptor valdesc = this.descriptors.get(this.currentIndex);
		    	
		    	valdesc.getHandlers().add(atts.getValue(0));
		    }else if(XMLEnum.HANDLERS.toString().equals(qName)) {
		    	XMLValidatorDescriptorImpl impl = (XMLValidatorDescriptorImpl)this.descriptors.get(this.currentIndex);
		    	
		    	impl.setHandlers(new HashSet<String>());
		    }
	  }


	/* (non-Javadoc)
	 * @see org.kopsox.util.xml.parser.impl.AbstractKopsoxXMLParser#getPathToDTD()
	 */
	@Override
	public String getPathToDTD() {
		return "org/kopsox/environment/proxy/config/xml/validatorsXMLDefinition.dtd";
	}
}
